﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace HXH.Redis
{
    public static class RedisServerCollectionExtensions
    {
        public static void AddRedisServer(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton<IDatabase>(sp=>
            {
                var redisConnection = configuration.GetSection("RedisConnectionString");
                if (string.IsNullOrEmpty(redisConnection.Value))
                {
                    throw new ArgumentNullException("RedisConnectionString");
                }
                var redisMultiplexer = ConnectionMultiplexer.Connect(redisConnection.Value);
                return redisMultiplexer.GetDatabase();
            });
            services.AddSingleton(typeof(IRedisClient),typeof(RedisClinet));
        }
    }
}
